# Edit this configuration file to define what should be installed on
# your system.  Help is available in the configuration.nix(5) man page
# and in the NixOS manual (accessible by running ‘nixos-help’).

{ config, lib, pkgs, ... }:

{
  imports = [
    # Guix
    /srv/git/os/nixos-services/guix.nix
  ];

  # 启用 Guix
  services.guix-daemon.enable = true;
  
  # Microcode
  hardware.cpu.intel.updateMicrocode = true;
  # binfmt
  boot.binfmt.emulatedSystems = [ 
	"aarch64-linux" "armv6l-linux" "armv7l-linux" 
	"mips-linux" "mipsel-linux" "mips64-linux" "mips64el-linux" 
	"riscv32-linux" "riscv64-linux" 
        ];

  # initrd 
  boot.initrd.availableKernelModules = [ "ehci_pci" "ahci" "usb_storage" "uas" "sd_mod" "sr_mod" "rtsx_usb_sdmmc" ];
  boot.initrd.kernelModules = [ ];
  boot.kernelModules = [ "kvm-intel" ];
  boot.extraModulePackages = [ ];

  # 文件系统
  fileSystems."/" =
    { device = "/dev/disk/by-uuid/518c36b7-1b3b-46ac-b2bf-b12f5f869b42";
      fsType = "btrfs";
      options = ["compress-force=zstd:15"];  # 启用压缩
    };

  fileSystems."/boot" =
    { device = "/dev/disk/by-uuid/ebe9edcc-0b09-4c14-b144-fcd2e64e8da9";
      fsType = "ext4";
    };

  # swap
  swapDevices = [ { device = "/var/swapfiles/0"; } ];

  # 内核参数
  boot.kernelParams = [ 
  "zswap.enabled=1"
  "zswap.compressor=lz4"
  "zswap.zpool=z3fold"
  ];

  # sysctl
  boot.kernel.sysctl = {
  # 使用BBR
  "net.core.default_qdisc" = "fq";
  "net.ipv4.tcp_congestion_control" = "bbr";
  # 禁用特权端口
  "net.ipv4.ip_unprivileged_port_start" = 0;
  };

  # CPU
  powerManagement.cpuFreqGovernor = lib.mkDefault "performance";

  # Nix
  nix.binaryCaches = lib.mkForce [ "https://mirrors.tuna.tsinghua.edu.cn/nix-channels/store" ];
  nix.maxJobs = lib.mkDefault 4;

  # 允许使用非自由软件
  nixpkgs.config.allowUnfree = true;

  # Grub
  boot.loader.grub.enable = true;
  boot.loader.grub.version = 2;
  boot.loader.grub.device = "/dev/sda"; 

  # 网络
  networking.hostName = "g470";
  networking.wireless.enable = true;
  networking.useDHCP = false;
  networking.interfaces.enp7s0.useDHCP = true;
  networking.interfaces.wlp8s0.useDHCP = true;
  # 启用wireguard
  networking.wireguard.enable = true;
  # 禁用自带防火墙
  networking.firewall.enable = false;
  # 启用nftables作为防火墙
  networking.nftables.enable = true;
  networking.nftables.rulesetFile = "/etc/nftables.rule";

  # locale
  i18n.defaultLocale = "en_US.UTF-8";
  # 控制台
  console = {
    font = "Lat2-Terminus16";
    keyMap = "us";
  };

  # 时区
  time.timeZone = "Hongkong";

  # 全局安装的包
  environment.systemPackages = with pkgs; [
    # 下载器
    wget curl 
    # 编辑器
    vim neovim 
    # 终端复用器
    tmux
    # 版本控制
    git
    # 增量文件复制
    rsync
    # 文件系统
    e2fsprogs btrfs-progs
    # btrfs 文件去重
    duperemove
    # 终端模拟器
    st 
    # 菜单
    dmenu
    # 图片查看器
    feh 
    # 截图
    scrot
    # 自动Xorg锁
    xautolock
    # 键盘操纵鼠标
    keynav
    # 资源监视器
    bmon iftop htop
    # 剪切版管理程序
    xclip
    # 视频播放器
    mplayer
    # 录制屏幕
    vokoscreen
    # 音乐播放器
    mpg123
    # status bar
    polybar
    # 浏览器
    google-chrome
    # Wireguard VPN
    wireguard
    wireguard-tools
  ];

  # slock
  programs.slock.enable = true;

  # 启用SSH
  services.openssh.enable = true;

  # 声音
  sound.enable = true;
  hardware.pulseaudio.enable = true;

  # 输入法
  i18n.inputMethod.enabled = "fcitx";
  i18n.inputMethod.fcitx.engines = with pkgs.fcitx-engines; [ rime ];

  # Xorg
  services.xserver.enable = true;
  services.xserver.layout = "us";
  services.xserver.libinput.enable = true;
  services.xserver.enableCtrlAltBackspace = true;
  services.xserver.useGlamor = true;
  services.xserver.videoDrivers =  [ "radeon" "amdgpu" "intel" ];

  # OpenGL
  hardware.opengl.enable = true;
  hardware.opengl.driSupport = true;
  hardware.opengl.extraPackages = with pkgs; [ vaapiIntel libvdpau-va-gl vaapiVdpau ];

  # 登录管理器
  services.xserver.displayManager.sddm.enable = true;
  # 窗口管理器
  services.xserver.windowManager.cwm.enable = true;
  
  # 字体
  fonts.fonts = [ pkgs.terminus_font pkgs.terminus_font_ttf pkgs.wqy_zenhei ];
  
  # 普通用户
  users.users.g470= {
    isNormalUser = true;
    extraGroups = [ "wheel" "vboxusers" ]; # Enable ‘sudo’ for the user.
  };

  # virtualbox
  virtualisation.virtualbox.host.enable = true;
  virtualisation.virtualbox.host.enableExtensionPack = true;
  virtualisation.virtualbox.host.enableHardening = true;
  
  # LXC
  virtualisation.lxc.enable = true;

  # ACPID
  services.acpid.enable = true;

  # This value determines the NixOS release from which the default
  # settings for stateful data, like file locations and database versions
  # on your system were taken. It‘s perfectly fine and recommended to leave
  # this value at the release version of the first install of this system.
  # Before changing this value read the documentation for this option
  # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
  system.stateVersion = "20.03"; # Did you read the comment?

}


